package com.qs.controller;

import com.qs.controller.dto.FormFieldDTO;
import com.qs.controller.vo.FormFieldVO;
import com.qs.mapper.entity.FormField;
import com.qs.service.FormFieldService;
import com.qs.service.bo.FormFieldBO;
import com.qs.util.BeanCopyUtils;
import com.qs.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/formField")
@RequiredArgsConstructor
public class FormFieldController {
    private final FormFieldService formFieldService;

    @GetMapping("/formName/{formName}")
    public R<List<FormFieldDTO>> getFormFieldByFormName(@PathVariable("formName") String formName) {
        List<FormFieldBO> formFieldBOList = formFieldService.getFormFieldByFormName(formName);
        return R.ok(BeanCopyUtils.copyBeanList(formFieldBOList, FormFieldDTO.class));
    }

    @GetMapping("/formId/{formId}")
    public R<List<FormFieldDTO>> getFormFieldByFormId(@PathVariable("formId") Long formId) {
        List<FormFieldBO> formFieldBOList = formFieldService.getFormFieldByFormId(formId);
        return R.ok(BeanCopyUtils.copyBeanList(formFieldBOList, FormFieldDTO.class));
    }


    @PostMapping
    public R<String> add(@RequestBody FormFieldVO formFieldVO) {
        FormFieldBO formFieldBO = BeanCopyUtils.copyBean(formFieldVO, FormFieldBO.class);
        String result = formFieldService.add(formFieldBO);
        return R.ok(result);
    }

    @PutMapping
    public R<String> edit(@RequestBody FormFieldVO formFieldVO) {
        FormFieldBO formFieldBO = BeanCopyUtils.copyBean(formFieldVO, FormFieldBO.class);
        String result = formFieldService.edit(formFieldBO);
        return R.ok(result);
    }

    @DeleteMapping("/{fieldId}")
    public R<String> delete(@PathVariable("fieldId") Long fieldId) {
        boolean removed = formFieldService.removeById(fieldId);
        String result = removed ? "删除成功" : "删除失败";
        return R.ok(result);
    }

}
